// pages/shouye/shouye.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 是否显示登录按钮
    needLogin: false,
    serach_pro: "",
    active: -1,
    // 轮播图
    list: [{
        name: "竞赛抢单",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/jsqd2.png",
      }, {
        name: "找企业",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/zqy2.png",
      },
      {
        name: "产业热力图",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/hotcy2.png",
      },
      {
        name: "看周边",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/kzb2.png",
      },
      {
        name: "行业洞悉",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/khy2.png",
      },
      {
        name: "银企对接",
        url: "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/shouye/bac_yinqi2.png",
      },
    ],
    currentLocation: "",
    originList: [],
    // 下面5个是轮播图配置
    indicatorDots: false,
    vertical: true,
    autoplay: true,
    interval: 3100,
    duration: 500,
    time: "",
    addressHotlist: [{
        area1: {
          location: {
            lat: "31.524564",
            lng: "120.563243"
          },
          region: "苏州市",
        },
        area2: {
          location: {
            lat: "31.32466",
            lng: "120.599241"
          },
          region: "姑苏区",
        },
      },
      {
        area1: {
          location: {
            lat: "31.846958",
            lng: "120.563915"
          },
          region: "张家港市",
        },
        area2: {
          location: {
            lat: "31.369189",
            lng: "120.642391"
          },
          region: "相城区",
        },
      },
      {
        area1: {
          location: {
            lat: "31.324235",
            lng: "120.718329"
          },
          region: "工业园区",
        },
        area2: {
          location: {
            lat: "31.273814",
            lng: "120.631414"
          },
          region: "吴中区",
        },
      },
      {
        area1: {
          location: {
            lat: "31.133259",
            lng: "120.643583"
          },
          region: "吴江区",
        },
        area2: {
          location: {
            lat: "31.656016",
            lng: "120.752512"
          },
          region: "常熟市",
        },
      },
      {
        area1: {
          location: {
            lat: "31.385476",
            lng: "120.980795"
          },
          region: "昆山市",
        },
        area2: {
          location: {
            lat: "31.458101",
            lng: "121.130366"
          },
          region: "太仓市",
        },
      },
      {
        area1: {
          location: '',
          region: "",
        },
        area2: {
          location: '',
          region: "",
        },
      },
    ],
    bankType: ''
  },
  // 跳转公共方法
  async toWhere(e) {
    const userInfo = wx.getStorageSync('userInfo') || ''
    const vipCode = await getApp().globalData.checkVipStatus('N')
    // if(await getApp().globalData.checkVipStatus()) return
    const name = e.currentTarget.dataset.name
    switch (name) {
      // 竞赛抢单
      case '竞赛抢单':
        wx.navigateTo({
          url: '../../sub_qiangdan/reservation/reservation',
        });
        break;
      case '看周边':
        wx.navigateTo({
          url: "/pages/map/map",
        });
        break;
      case '找企业':
        // wx.navigateTo({
        //   url: "/sub_first_credit/shouye/shouye",
        // });
        wx.navigateTo({
          url: "/sun_other/Arrorney_copy/Arrorney_copy",
        });
        break;
      case '全量搜':
        wx.navigateTo({
          url: '/sun_chaifen/advancedSearch/advancedSearch?type=1',
        });
        break;
      case '银企对接':
        if (userInfo) {
          const vipCode = await getApp().globalData.checkVipStatus()
          console.log(vipCode, 'vipOcde');
          if (vipCode === 'bindGOV') {
            wx.showToast({
              title: '暂无相关权限',
              icon: 'none'
            })
            return
          }
          if (userInfo.corpRole.includes('cbrc') || vipCode === 'administrator') {
            wx.navigateTo({
              url: "/pages/qiyezoufang/qiyezoufang",
            });
          } else {
            if (userInfo && userInfo.agencyCode) {
              wx.navigateTo({
                url: "/pages/qiyezoufang/qiyezoufang",
              });
            } else {
              wx.navigateTo({
                url: '/pages/accountBind/accountBind?type=3'
              })
            }
          }
        } else {
          wx.switchTab({
            url: '/pages/personalCenter/personalCenter',
          })
        }
        break;
      case '行业洞悉':
        wx.navigateTo({
          url: "/packageA/pages/homelink/analyseTrade/analyseTrade",
        });
        break;
      case '产业热力图':
        wx.navigateTo({
          url: "/pages/cyHotMap/cyHotMap",
        });
        break;
      case '关联图谱':
        wx.navigateTo({
          url: "/sub_guanlian/page1/page1",
        });
        break;
      case '测额度':
        wx.navigateTo({
          url: "/sub_celilv/measureCelilv/measureCelilv",
        });
        break;
      case '利率计算器':
        wx.navigateTo({
          url: "/sub_celilv/measureCelilv/measureCelilv?type=1",
        });
        break;
      case '特色企业':
        wx.navigateTo({
          url: "/sub_teseqiye/page1/page1",
        });
        break;
      case '科创评分':
        wx.navigateTo({
          url: "/sub_celilv/InnovationScore/InnovationScore",
        });
        break;
      case '行业洞悉':
        wx.navigateTo({
          url: "/packageA/pages/homelink/analyseTrade/analyseTrade",
        });
        break;
      case '巡检':
        if (vipCode === 'administrator') {
          wx.navigateTo({
            url: "/sub_xunjian/page0/page0",
          });
        } else {
          if (userInfo && userInfo.isSurvey == 1) {
            wx.navigateTo({
              url: "/sub_xunjian/page0/page0",
            });
          } else {
            wx.showToast({
              icon: "none",
              title: "暂未开放",
            });
          }
        }
        break;
      case '苏州金服':
        wx.showToast({
          icon: "none",
          title: "暂未开放",
        });
        // wx.navigateTo({
        //   url: "/sub_jinfu/all_list/all_list",
        // });
        break;
      case '小征':
        // wx.showToast({
        //   icon: "none",
        //   title: "暂未开放",
        // });
        let corpRole = wx.getStorageSync('userInfo').corpRole
        if (corpRole.includes('chat_user')) {
          wx.navigateTo({
            url: '/pages/chatGpt/chatGpt'
          })
        } else {
          wx.showToast({
            icon: "none",
            title: "暂未开放",
          });
        }
        break;
      case '风险预警':
        wx.navigateTo({
          url: '/sub_monitor/attention/attention'
        })
        break;
      case '窥经营':
        wx.navigateTo({
          url: "/pages/kuijingying/shouye/shouye",
        });
        break;
      case '识空壳':
        wx.navigateTo({
          url: "/packageA/pages/homelink/identifyShell/identifyShell",
        });
        break;
      case '营销中心':
        if (userInfo && userInfo.phone) {
          if (userInfo && userInfo.agencyCode) {
            wx.navigateTo({
              url: "/sub_marketing/roll_call/roll_call",
            });
          } else {
            wx.navigateTo({
              url: '/pages/accountBind/accountBind?type=3'
            })
          }
        } else {
          wx.showToast({
            title: '请先手机号登录',
            icon: 'none'
          })
        }
        break;
      case '科创筛选':
        wx.navigateTo({
          url: "/sub_celilv/kechuangfilter/kechuangfilter",
        });
        break;
      default:
        break;
    }
  },
  // 去登录
  tologinClick() {
    wx.setStorageSync("token", "cxgc123456");
    wx.navigateTo({
      url: "/pages/launch_start/launch_start",
    });
  },
  serachProChange(e) {
    console.log(e);
    this.setData({
      serach_pro: e.detail.value,
    });
  },
  // 去全量搜索
  toseracg_allpro() {
    wx.navigateTo({
      url: "/packageA/pages/homelink/zhaoqiye/zhaoqiye",
    });
  },
  async toYqdj() {
    // wx.showToast({
    //   title: "近期上线，敬请期待！",
    //   icon: "none",
    // });
    // wx.navigateTo({
    //   url: '/pages/qiyezoufang/qiyezoufang',
    // })
    // 获取个人信息

    const token = wx.getStorageSync("token");
    if (token === "cxgc123456") {
      wx.navigateTo({
        url: "/pages/qiyezoufang/qiyezoufang",
      });
    } else {
      const {
        data
      } = await getApp().globalData.request(
        "/login/getCurrentUserInfo", {
          needToken: true,
        },
        "POST"
      );
      console.log(data, "data");
      wx.setStorageSync("userInfo", data);
      wx.navigateTo({
        url: "/pages/qiyezoufang/qiyezoufang",
      });
    }
  },

  goOneArea(e) {
    const region = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/cyHotDetail/cyHotDetail?latitude=${region.location.lat}&longitude=${region.location.lng}&region=${region.region}`,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  toChat() {
    wx.navigateTo({
      url: '/pages/chatGpt/chatGpt'
    })
  },

  // 定制服务
  toSpec() {
    const userInfo = wx.getStorageSync('userInfo') || ''
    // if (userInfo) {
    //   const bankType = userInfo.bankType
    //   switch (bankType) {
    //     case 'boc':
    //       wx.navigateTo({
    //         url: "/sub_service/zhongguo/index",
    //       });
    //       break;
    //     case 'jsyh':
    //       wx.navigateTo({
    //         url: '/sub_service/jiangsu/index',
    //       })
    //       break;
    //     case 'zdyh':
    //       wx.navigateTo({
    //         url: '/sub_service/zhada/index',
    //       })
    //       break;
    //     default:
    //       break;
    //   }
    // }
    wx.switchTab({
      url: '/pages/service/service',
    })
  },


  toprodetail() {
    this.tokanzhoubianClick();
  },

  async getNationalEconomyChart(key) {
    console.log(key, "key");
    const params = {
      key: key,
    };
    const {
      data
    } = await getApp().globalData.request(
      "/map/nationalEconomyChart",
      params,
      "POST"
    );
    this.setData({
      allData: {
        years: [""],
        xData: data.timeList,
        yData: data.dataList,
        title: "",
        type: "line",
      },
    });
    console.log(data, "趋势图数据");
  },

  checkOpenId() {
    const userInfo = wx.getStorageSync('userInfo') || ''
    if (userInfo) {
      if (userInfo.phone && !userInfo.openId) {
        // 获取openId并且保存
        wx.login({
          success: res => {
            const params = {
              code: res.code,
              token: wx.getStorageSync('token')
            }
            console.log(params, 'wx.login');
            getApp().globalData.request(
              "/user/bindOpenId",
              params,
              "POST"
            );
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // openId存储
    this.checkOpenId();
    const token = wx.getStorageSync("token");
    this.setData({
      needLogin: token ? false : true,
    });
    // 更新vip信息
    getApp().globalData.vipCode = wx.getStorageSync('vipCode') || ''
    if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        console.log("onCheckForUpdate====", res);
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log("res.hasUpdate====");
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function (res) {
                console.log("success====", res);
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                  wx.removeStorageSync('token')
                }
              },
            });
          });
          updateManager.onUpdateFailed(function () {
            wx.removeStorageSync('token')
            // 新的版本下载失败
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
            });
          });
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});